
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by besti on 2018/6/9.
 */
public class SocketServer {
    public static void main(String[] args) throws IOException {
        //1.建立一个服务器Socket(ServerSocket)绑定指定端口
        ServerSocket serverSocket=new ServerSocket(8800);
        //2.使用accept()方法阻止等待监听，获得新连接
        Socket socket=serverSocket.accept();
        //3.获得输入流
        InputStream inputStream=socket.getInputStream();
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
        //获得输出流
        OutputStream outputStream=socket.getOutputStream();
        PrintWriter printWriter=new PrintWriter(outputStream);
        //4.读取用户输入信息
        String info=null;
        if (!((info = bufferedReader.readLine()) ==null)){
            System.out.println("我是服务器，用户信息为：" + info);
        }

        String[] Input = info.split(" ");
        int[] input = new int[Input.length];
        for (int i = 0;i <Input.length;i++)
        {
            input[i] = Integer.parseInt(Input[i]);
        }
        SelectionSort.selectionSort(input);
        String Output ="";
        for (int output : input)
        {
            Output+=output + " ";
        }

        //给客户一个响应
        String reply=Output;
        printWriter.write(reply);
        printWriter.flush();
        //5.关闭资源
        printWriter.close();
        outputStream.close();
        bufferedReader.close();
        inputStream.close();
        socket.close();
        serverSocket.close();
    }
    public static class SelectionSort {

        public static void selectionSort(int[] a) {
            int n = a.length;
            for (int i = 0; i < n; i++) {
                int k = i;
                // 找出最小值的小标
                for (int j = i + 1; j < n; j++) {
                    if (a[j] < a[k]) {
                        k = j;
                    }
                }
                // 将最小值放到排序序列末尾
                if (k > i) {
                    int tmp = a[i];
                    a[i] = a[k];
                    a[k] = tmp;
                }
            }
        }
    }
}